/**
 * File Name   : ProductTypeService.java
 * Description : 
 */
package com.hh.admin.producttype.service;

import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.hh.admin.producttype.form.ParamProductTypeBean;
import com.hh.admin.producttype.form.ProductTypeForm;
import com.hh.admin.producttype.form.ResultProductTypeBean;
import com.hh.common.ibatis.dao.IQueryDao;
import com.hh.common.ibatis.dao.IUpdateDao;
import com.hh.portal.products.form.ParamPortalProductsBean;

/**
 * 产品类型管理Service
 * @author 冯昌硕
 * @create 2013-1-1 下午1:24:27
 * @modify 2013-01-20 15:33 删除分页操作的方法getProductTypeList，改用CommonService的公共查询方法
 * @modify 2013-01-20 15:55 删除方法getProductTypeTotleCnt，改用CommonService的公共查询方法
 */
public class ProductTypeService {
    
    private static final Logger log = Logger.getLogger(ProductTypeService.class);

    private IQueryDao queryDao;
    
    private IUpdateDao updateDao;
    
    /**
     * 删除产品类型记录
     * @param id 产品类型ID
     * @return
     * @throws SQLException
     * @create 2013-1-1 下午3:24:24
     * @modify 2013-02-16 删除单条产品类型对应删除common_product记录
     */
    public int deleteOneProducttype(int id) throws SQLException {
        int delRs = -1;
        try {
            updateDao.startTransaction();
            delRs = updateDao.delete("producttype.deleteProductType", id);
            log.debug("[deleteOneProducttype] productTypeId=[" + id + "] [producttype.deleteProductType删除影响记录行数[" + delRs+ "]]");
            
            ParamPortalProductsBean paramBean = new ParamPortalProductsBean();
            paramBean.setProductTypeId(id);
            int delRs2 = updateDao.delete("commonProduct.deleteCommonProductByType", paramBean);
            log.debug("[deleteOneProducttype] productTypeId=[" + id + "] [commonProduct.deleteCommonProductByType删除影响记录行数[" + delRs2 + "]]");
            
            updateDao.commitTransaction();
        } finally {
            updateDao.endTransaction();
        }
        
        //return updateDao.delete("producttype.deleteProductType", id);
        return delRs;
    }
    
    /**
     * 批量删除产品类型记录
     * @param ids 要删除的产品类型ID
     * @return
     * @throws SQLException
     * @create 2013-1-1 下午3:29:33
     * @modify 2013-02-16 16:00 删除批量产品类型对应删除common_product记录
     */
    public int deleteManyProducttype(Integer[] ids) throws SQLException {
        int result = -1;
        
        try {
            updateDao.startTransaction();
            updateDao.startBatch();
            
            if((ids == null) || (ids.length <= 0)) {
                log.info("批量删除[producttype.deleteProductType]参数数组为空，不作处理");
                return result;
            }
            
            // 批处理删除内容表记录
            for(Object paramObj : ids) {
                updateDao.delete("producttype.deleteProductType", paramObj);
            }
            
            // 批处理删除公共内容表记录
            for(Integer paramObj : ids) {
                ParamPortalProductsBean paramBean = new ParamPortalProductsBean();
                paramBean.setProductTypeId(paramObj);
                updateDao.delete("commonProduct.deleteCommonProductByType", paramBean);
            }
            
            result = updateDao.executeBatch();
            
            updateDao.commitTransaction();
        } catch(SQLException e) {
            log.error("[deleteManyProducttype] 批处理异常： " + e.toString());
            throw e;
        } finally {
            updateDao.endTransaction();
        }
        
        //return updateDao.batchDelete("producttype.deleteProductType", ids);
        return result;
    }
    
    /**
     * 获取产品类型记录详情
     * @param id 产品类型ID
     * @return 产品类型详情
     * @create 2013-1-1 下午3:39:02
     */
    public ResultProductTypeBean getProductTypeDetail(int id) throws ClassCastException, SQLException {
        return queryDao.queryForObject("producttype.selProductTypeDetail", id, ResultProductTypeBean.class);
    }
    
    /**
     * 更新产品类型信息
     * @param form 表单项
     * @return
     * @throws SQLException
     * @create 2013-1-1 下午3:52:44
     * @modify 2013-02-16 22:06 更新时添加字段TypeFlag
     */
    public int updateProductType(ProductTypeForm form) throws SQLException {
        ParamProductTypeBean paramBean = new ParamProductTypeBean();
        paramBean.setId(form.getId());
        paramBean.setStatus(form.getStatus());
        paramBean.setTypeName(form.getTypeName());
        paramBean.setCreateUser(form.getCreateUser());
        paramBean.setUrl(form.getUrl());
        paramBean.setTypeFlag(form.getTypeFlag());
        
        log.debug("更新产品类型信息 paramBean=[" + paramBean + "]");
        return updateDao.update("producttype.undateProducttype", paramBean);
    }
    
    /**
     * 添加产品类型单记录
     * @param form
     * @return
     * @throws SQLException
     * @create 2012-12-27 下午9:09:40
     * @modify 2013-02-16 22:03 保存时添加字段TypeFlag
     */
    public int insertProducttype(ProductTypeForm form) throws SQLException {
        ParamProductTypeBean paramBean = new ParamProductTypeBean();
        paramBean.setStatus(form.getStatus());
        paramBean.setTypeName(form.getTypeName());
        paramBean.setCreateUser(form.getCreateUser());
        paramBean.setUrl(form.getUrl());
        paramBean.setTypeFlag(form.getTypeFlag());
        
        log.debug("新增产品类型信息 paramBean=[" + paramBean + "]");
        return (Integer) updateDao.insert("producttype.insertProducttype", paramBean);
    }
    
    /**
     * 根据产品类型标识查询产品类型ID
     * @param typeFlag
     * @return
     * @throws ClassCastException
     * @throws SQLException
     * @create 2013-2-22 下午8:36:09
     */
    public int getProductTypeIdByFlag(String typeFlag) throws ClassCastException, SQLException {
        return queryDao.queryForObject("producttype.getProductTypeIdByFlag", typeFlag, Integer.class);
    }
    
    /**
     * 根据产品类型ID查询产品类型标识
     * @param id
     * @return
     * @throws ClassCastException
     * @throws SQLException
     * @create 2013-2-22 下午8:37:13
     */
    public String getProductFlagById(int id) throws ClassCastException, SQLException {
        return queryDao.queryForObject("producttype.getProductTypeFlagById", id, String.class);
    }

    public void setQueryDao(IQueryDao queryDao) {
        this.queryDao = queryDao;
    }

    public void setUpdateDao(IUpdateDao updateDao) {
        this.updateDao = updateDao;
    }
}
